package com.quanyan.user.service.impl;


import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.entity.TbUserAssets;
import com.quanyan.assets.request.ReqUserInfo;
import com.quanyan.assets.service.UserAssetsService;
import com.quanyan.common.constants.Constants;
import com.quanyan.user.mapper.TbLevelParamMapper;
import com.quanyan.user.model.db.TbLevel;
import com.quanyan.user.model.db.TbLevelParam;
import com.quanyan.user.model.db.TbLevelParamExample;
import com.quanyan.user.model.vo.response.RespTaskCenter;
import com.quanyan.user.model.vo.response.RespUserCenter;
import com.quanyan.user.model.vo.response.RespUserLevelExp;
import com.quanyan.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2016/7/2.
 */
@Service
public class UserTaskCenterServiceImpl implements UserTaskCenterService {

    @Autowired
    private UserTreasureBoxService userTreasureBoxService;

    @Autowired
    private UserTaskService userTaskService;

    @Autowired
    private ReceiveDetailService receiveDetailService;

    @Autowired
    private UserAssetsService userAssetsService;

    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private TbLevelParamMapper tbLevelParamMapper;

    @Autowired
    private LevelService levelService;


    @Override
    public RespUserCenter queryTaskAndBox(Integer uid){
        RespUserCenter respUserCenter = new RespUserCenter();
        int isComplete = userTaskService.isNewHandComplete(uid);
        respUserCenter.setNewHandIsComplete(isComplete);
        respUserCenter.setRespDayUserTaskList(userTaskService.queryUserDayTask(uid));//日常任务
        respUserCenter.setIsTaoEgg(userTaskService.isTaoEgg(uid));
        Long currentDayReceiveEggShellNum = receiveDetailService.getCurrentDayReceiveEggShellNum(uid);
        respUserCenter.setRespTreasureBox(userTreasureBoxService.queryUserBox(uid, currentDayReceiveEggShellNum));
        if(currentDayReceiveEggShellNum != null)
            respUserCenter.setReceiveEggShellNum(new BigDecimal(currentDayReceiveEggShellNum));

        APIRequest<ReqUserInfo> apiRequest = new APIRequest<ReqUserInfo>();
        ReqUserInfo r= new ReqUserInfo();
        r.setUid(uid);
        r.setAssetsType(AssetsType.ASSETS_SHELL.getId().byteValue());
        apiRequest.setData(r);
        APIResponse<List<TbUserAssets>> response = userAssetsService.getUserAccumulateList(apiRequest);
        if (response.getData() != null){
            TbUserAssets assets = response.getData().get(0);
            if(assets.getAccumulate() != null)
                respUserCenter.setEggShellNum(new BigDecimal(assets.getAccumulate()));
        }

        return respUserCenter;
    }


    @Override
    public RespTaskCenter getTaskCenter(Integer uid) {
        RespTaskCenter respTaskCenter = new RespTaskCenter();
        int isComplete = userTaskService.isNewHandComplete(uid);//1 完成 0 未完成
        respTaskCenter.setNewHandIsComplete(isComplete);
        respTaskCenter.setDayTask(userTaskService.queryUserDayTask(uid));
        if (isComplete == 0){ // 未完成
            respTaskCenter.setNewcomerTask(userTaskService.queryUserNewHandTask(uid));
        }

        TbLevel level = userInformationService.getLevel(uid, Constants.LevelType.USER_LEVEL);
        int lev = level.getLevel();//当前级别
        int levXp = 0;// 等级经验

        TbLevelParamExample currentLevelExample = new TbLevelParamExample();
        TbLevelParamExample.Criteria criteriaCurrentLevel = currentLevelExample.createCriteria();
        criteriaCurrentLevel.andLevelEqualTo(lev);
        criteriaCurrentLevel.andTypeEqualTo(Constants.LevelType.USER_LEVEL);
        criteriaCurrentLevel.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        List<TbLevelParam> lstCurrentLevXp = tbLevelParamMapper.selectByExample(currentLevelExample); //当前级别的经验
        if (lstCurrentLevXp != null && lstCurrentLevXp.size() > 0){
            levXp = lstCurrentLevXp.get(0).getXp()                                                                                                                                                                                                                                                        ;
        }

        int userXp = level.getXp(); //用户经验
        int nextLevelXp = level.getNextLevelXp();//下个经验
        Integer diffXp = nextLevelXp - levXp; // 两等级之间的经验
        Integer tmpXp =  userXp - levXp ; //等级之间相差经验

        Double scale = tmpXp.doubleValue() / diffXp.doubleValue() ;
        RespUserLevelExp respUserLevelExp = new RespUserLevelExp();
        TbLevelParam tbLevelParam = levelService.getHighLevel();
        if(tbLevelParam != null){
            respUserLevelExp.setHighLevel(tbLevelParam.getLevel());
            respUserLevelExp.setHighXp(tbLevelParam.getXp());
        }
        respUserLevelExp.setXp(userXp);
        respUserLevelExp.setLev(lev);
        respUserLevelExp.setScale(scale);
        respTaskCenter.setRespUserLevelExp(respUserLevelExp);
        return respTaskCenter;
    }
}
